/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.model;

import com.wisnu.spms.service.dao.DbStudentDao;
import com.wisnu.spms.service.entity.DbStudent;
import com.wisnu.spms.service.util.SpringUtil;
import com.wisnu.spms.source.listener.PanelStudentListener;
import java.util.List;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelStudentModel {

    private Integer id;
    private String keyword;
    private DbStudent student;

    PanelStudentListener listener;

    public PanelStudentModel(PanelStudentListener listener) {
        this.listener = listener;
    }

    public void setStudent(DbStudent student) {
        this.student = student;
        onChange(this);
    }

    public DbStudent getStudent() {
        return student;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public List<DbStudent> searchStudentByKeyword() throws Exception {
        DbStudentDao studentDao = SpringUtil.getDbStudentDao();
        return studentDao.getStudentsByKeyword(keyword);

    }

    public void searchStudentById() throws Exception {
        DbStudentDao studentDao = SpringUtil.getDbStudentDao();
        setStudent(studentDao.getStudentById(id));
    }

    public void addStudent() throws Exception {
        DbStudentDao studentDao = SpringUtil.getDbStudentDao();
        studentDao.insertStudent(student);
        onChange(this);
    }

    public void updateStudent() throws Exception {
        DbStudentDao studentDao = SpringUtil.getDbStudentDao();
        studentDao.updateStudent(student);
        onChange(this);
    }

    public void onChange(PanelStudentModel model) {
        listener.onChange(model);
    }

}
